﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.malls.MarketUI.model {
    import flash.geom.*;
    import Mechanics.GameMechanicInterfaces.*;
    import Events.*;
    import Display.DialogUI.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Classes.effects.*;
    import org.aswing.geom.*;
    import Display.*;
    import Display.MarketUI.*;
    import GameMode.*;
    import Classes.gates.*;
    import Modules.crew.*;
    import Display.GridlistUI.model.*;
    import Modules.malls.ui.*;
    import Modules.malls.MarketUI.view.*;

    public class MallCellModel extends ItemInstanceCellModel {

        protected var m_crewMember:String;

        override protected function loadAssets():void{
            this.m_assetsToLoad = 3;
            Global.delayedAssets.get(DelayedAssetLoader.MARKET_ASSETS, onAssetsLoaded);
            Global.delayedAssets.get(DelayedAssetLoader.MALL_ASSETS, onAssetsLoaded);
            Global.delayedAssets.get(DelayedAssetLoader.INVENTORY_ASSETS, onAssetsLoaded);
        }
        public function onRemove():void{
            var _local1:IMechanicUser = (getCellComponent() as MallCellView).generator;
            var _local2:Array = _local1.getDataForMechanic("slots");
            var _local3:int = MallCatalogUI.NUM_SLOTS_PER_FLOOR;
            var _local4:int = (getCellComponent() as MallCellView).tabId;
            var _local5:int = ((_local3 * _local4) + m_index);
            if ((((((m_index < 0)) || ((_local5 < 0)))) || ((_local5 >= _local2.length)))){
                return;
            };
            Global.world.addGameMode(new GMMechanicPlaceFromStorage(_local1, _local5, (m_value.data as ItemInstance)));
        }
        public function onAskCrew():void{
            var _local1:MechanicMapResource = ((getCellComponent() as MallCellView).generator as MechanicMapResource);
            var _local2 = "storage";
            var _local3:String = ("crew.php?mId=" + _local1.getId().toString());
            if (((!((_local2 == null))) && (!((_local2 == ""))))){
                _local3 = (_local3 + ("&gname=" + _local2));
            };
            FrameManager.navigateTo(_local3);
        }
        public function onBuyCrew():void{
            var mechanicUser:* = null;
            mechanicUser = ((getCellComponent() as MallCellView).generator as MechanicMapResource);
            UI.displayMessage(ZLoc.t("Dialogs", "Malls_buyQuestion", {amount:String(CrewGate.getCrewCost(mechanicUser.getItemName(), "storage"))}), GenericDialogView.TYPE_YESNO, function (_arg1:GenericPopupEvent):void{
                var _local2:int;
                if (_arg1.button == GenericDialogView.YES){
                    _local2 = CrewGate.getCrewCost(mechanicUser.getItemName(), "storage");
                    if (Global.player.canBuyCash(_local2, true)){
                        GameTransactionManager.addTransaction(new TBuyCrew(mechanicUser.getId(), 0, _local2, "storage"), true);
                        Global.world.addGameMode(new GMPlay());
                    };
                };
            });
        }
        public function getCrewMemberForIdx(_arg1:int, _arg2:int):String{
            var _local3:MechanicMapResource = ((getCellComponent() as MallCellView).generator as MechanicMapResource);
            var _local4:String = GameWorld.CITY_SAM_OWNER_ID;
            var _local5:Crew = Global.crews.getCrewById((_local3 as MechanicMapResource).getId());
            var _local6:int = _local3.getItem().storageInitCapacity;
            var _local7:int = MallCatalogUI.NUM_SLOTS_PER_FLOOR;
            var _local8:int = ((_arg2 + (_arg1 * _local7)) - _local6);
            if (((((_local5) && ((_local8 >= 0)))) && ((_local8 < _local5.count)))){
                if (Number(_local5.list[_local8]) > 0){
                    _local4 = _local5.list[_local8];
                };
            };
            return (_local4);
        }
        public function onDisplayBusinessTooltip():void{
            var _local1:Business = (getCellValue() as Business);
            if (_local1 == null){
                return;
            };
            var _local2:MallBusinessCellView = (getCellComponent() as MallBusinessCellView);
            var _local3:int = _local2.tabId;
            var _local4:int = _local2.slotId;
            var _local5:IntPoint = _local2.getGlobalLocation();
            var _local6:Point = new Point(_local5.x, _local5.y);
            var _local7:Object = new Object();
            _local7["type"] = TooltipType.BUSINESS;
            _local7["title"] = _local1.getItem().localizedName;
            _local7["item"] = _local1.getItem();
            _local7["crewName"] = Global.player.getFriendName(this.getCrewMemberForIdx(_local3, _local4));
            _local7["upgradeLevel"] = _local1.getItem().level;
            _local7["upgradeProgress"] = (_local1.canCountUpgradeActions()) ? _local1.upgradeActions.getTotal() : -1;
            var _local8:Point = new Point(15, -20);
            var _local9:DataItemEvent = new DataItemEvent(DataItemEvent.SHOW_TOOLTIP, null, _local6, true);
            _local9.setParams(_local7);
            _local9.setOffset(_local8);
            _local2.dispatchEvent(_local9);
        }
        public function onDisplayEmptyCellTooltip():void{
            var _local1:MallBusinessCellView = (getCellComponent() as MallBusinessCellView);
            var _local2:int = _local1.tabId;
            var _local3:int = _local1.slotId;
            var _local4:IntPoint = _local1.getGlobalLocation();
            var _local5:Point = new Point(_local4.x, _local4.y);
            var _local6:String = this.getCrewMemberForIdx(_local2, _local3);
            var _local7:String = Global.player.getFriendName(_local6);
            var _local8:Object = new Object();
            _local8["type"] = TooltipType.DESCRIPTION;
            _local8["title"] = ZLoc.t("Dialogs", "MallEmptyCellTitle");
            _local8["description"] = ZLoc.t("Dialogs", "MallEmptyCellText");
            if (_local7 != null){
                _local8["requirement"] = ZLoc.t("Dialogs", "MallBusinessCrew", {crew:_local7});
            };
            var _local9:Point = new Point(15, -20);
            var _local10:DataItemEvent = new DataItemEvent(DataItemEvent.SHOW_TOOLTIP, null, _local5, true);
            _local10.setParams(_local8);
            _local10.setOffset(_local9);
            _local1.dispatchEvent(_local10);
        }
        public function onDisplayMysteryCellTooltip():void{
            var _local1:MallMysteryCellView = (getCellComponent() as MallMysteryCellView);
            var _local2:IntPoint = _local1.getGlobalLocation();
            var _local3:Point = new Point(_local2.x, _local2.y);
            var _local4:Object = this.getMysteryCellToolTipParams();
            var _local5:Point = new Point(17, -20);
            var _local6:DataItemEvent = new DataItemEvent(DataItemEvent.SHOW_TOOLTIP, null, _local3, true);
            _local6.setParams(_local4);
            _local6.setOffset(_local5);
            _local1.dispatchEvent(_local6);
        }
        protected function getMysteryCellToolTipParams():Object{
            var _local1:int = (getCellComponent() as MallCellView).tabId;
            var _local2:Mall = ((getCellComponent() as MallCellView).generator as Mall);
            var _local3:Object = {};
            _local3["type"] = TooltipType.DESCRIPTION;
            var _local4:int = ((_local1 + 1) * MallCatalogUI.NUM_SLOTS_PER_FLOOR);
            var _local5:int = ((_local2.slots)!=null) ? _local2.slots.length : 0;
            if (_local2.hasMysteryBusinessInInventory(_local1)){
                _local3["title"] = ZLoc.t("Dialogs", "Congratulations");
                _local3["description"] = ZLoc.t("Dialogs", "MallMysteryCell_Tooltip_desc1");
            } else {
                if (_local5 < _local4){
                    _local3["title"] = ZLoc.t("Dialogs", "MallMysteryCell_Tooltip_title2");
                    _local3["description"] = ZLoc.t("Dialogs", "MallMysteryCell_Tooltip_desc2");
                    _local3["requirement"] = ZLoc.t("Dialogs", "MallMysteryCell_Tooltip_reqs2", {num:_local4});
                } else {
                    _local3["title"] = ZLoc.t("Dialogs", "MallMysteryCell_Tooltip_title3");
                    _local3["description"] = ZLoc.t("Dialogs", "MallMysteryCell_Tooltip_desc3");
                };
            };
            return (_local3);
        }
        public function onDisplayCrewableCellTooltip():void{
            var _local1:MallCrewableCellView = (getCellComponent() as MallCrewableCellView);
            var _local2:IntPoint = _local1.getGlobalLocation();
            var _local3:Point = new Point(_local2.x, _local2.y);
            var _local4:Object = new Object();
            _local4["type"] = TooltipType.DESCRIPTION;
            _local4["title"] = ZLoc.t("Dialogs", "MallCrewableCellTitle");
            _local4["description"] = ZLoc.t("Dialogs", "MallCrewableCellDescription");
            _local4["requirement"] = ZLoc.t("Dialogs", "MallCrewCrewableCellReq");
            var _local5:Point = new Point(15, -20);
            var _local6:DataItemEvent = new DataItemEvent(DataItemEvent.SHOW_TOOLTIP, null, _local3, true);
            _local6.setParams(_local4);
            _local6.setOffset(_local5);
            _local1.dispatchEvent(_local6);
        }
        public function onEnterStoreMode():void{
            var _local1:IMechanicUser = (getCellComponent() as MallCellView).generator;
            Global.world.addGameMode(new GMMechanicStore(_local1));
        }
        public function onExitStoreMode():void{
            Global.world.addGameMode(new GMPlay());
        }
        public function onUpgrade():void{
            var _local3:Object;
            var _local4:TStoredObjectAction;
            var _local1:Mall = ((getCellComponent() as MallCellView).generator as Mall);
            var _local2:Business = (getCellValue() as Business);
            if (((!((_local1 == null))) && (!((_local2 == null))))){
                _local3 = new Object();
                _local3["storageId"] = _local1.getId();
                _local3["storedObjectId"] = _local2.getId();
                if (_local2.isUpgradePossible()){
                    _local4 = new TStoredObjectAction(TStoredObjectAction.UPGRADE, null, _local3);
                    _local2.upgradeBuildingIfPossible(false, _local4, false);
                    this.playUpgradeEffects();
                };
                Global.ui.dispatchEvent(new GenericObjectEvent(GenericObjectEvent.STORED_ITEM_UPGRADE, null));
            };
        }
        private function playUpgradeEffects():void{
            var mall:* = null;
            mall = ((getCellComponent() as MallCellView).generator as Mall);
            var numEffect:* = 3;
            var interval:* = 200;
            MapResourceEffectFactory.createEffect(mall, EffectType.FIREWORK_BALLOONS);
            Sounds.play("cruise_fireworks");
            var effectsInstantiated:* = 0;
            while (effectsInstantiated <= numEffect) {
                TimerUtil.callLater(function ():void{
                    MapResourceEffectFactory.createEffect(mall, EffectType.FIREWORK_BALLOONS);
                }, (effectsInstantiated * interval));
                effectsInstantiated = (effectsInstantiated + 1);
            };
        }

    }
}//package Modules.malls.MarketUI.model 
